package com.sxkiler.demo.maintest.management;

import java.lang.management.*;
import java.util.*;
import com.sxkiler.demo.model.*;

public class JStackInfoService {
    public static void main(String args[]) throws InterruptedException {
        getOsInfo();
        getRuntimeMXBean();
        getGCMXbean();
        getMemoryMXBean();
        getMemoryManagerMXBean();
        getStatckInfo(Thread.currentThread().getId());
        Thread.sleep(3000000);
    }
    public static void getStatckInfo(long idFind){
        Thread thrd = null;
        Map<Thread,StackTraceElement[]> map = Thread.getAllStackTraces();
        Set tt = map.keySet();
        Iterator<Thread> ti = tt.iterator();
        try{
            System.out.println("<br> idFind thread |" + idFind +"| <br>");
            boolean found = false;
            while(ti.hasNext() ){
                thrd = ti.next();
                if(thrd.getId() == idFind){
                    System.out.println("<br> find thread" + thrd +" <br>");
                    break;
                }
            }
            if(!found){
                System.out.print("<br>not found" );

            }
        }catch(Exception e){
            System.out.println("<br>err" + e +"<br>");
        }

    }
    public static void getThreadMxBean(){
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        ThreadInfo threadInfo = threadBean.getThreadInfo(Thread.currentThread().getId());
        StackTraceElement[] stackList = threadInfo.getStackTrace();
        for(StackTraceElement stack:stackList){

        }
    }
    public static void getMemoryMXBean(){
        MemoryMXBean memMXBean = ManagementFactory.getMemoryMXBean();
        System.out.println("======getMemoryMXBean=======");
        System.out.println("getHeapMemoryUsage="+memMXBean.getHeapMemoryUsage());
        System.out.println("getNonHeapMemoryUsage="+memMXBean.getNonHeapMemoryUsage());
        System.out.println("getObjectPendingFinalizationCount="+memMXBean.getObjectPendingFinalizationCount());
        System.out.println("==========================================");
    }

    public static void getGCMXbean(){
        System.out.println("======getGCMXbean=======");
        List<GarbageCollectorMXBean> list = ManagementFactory.getGarbageCollectorMXBeans();
        for(GarbageCollectorMXBean gcMXbean:list){
            System.out.println("getName="+gcMXbean.getName());
            System.out.println("getCollectionCount="+gcMXbean.getCollectionCount());
            System.out.println("getCollectionTime="+gcMXbean.getCollectionTime());
            System.out.println("getMemoryPoolNames="+gcMXbean.getMemoryPoolNames());
            System.out.println("getObjectName="+gcMXbean.getObjectName());
            System.out.println("==========================================");
        }
    }
    public static void getMemoryManagerMXBean(){
        System.out.println("======getMemoryManagerMXBean=======");
        List<MemoryManagerMXBean> list = ManagementFactory.getMemoryManagerMXBeans();
        for(MemoryManagerMXBean mmMXBean:list){
            System.out.println("getName="+mmMXBean.getName());
            System.out.println("getMemoryPoolNames="+ Arrays.asList(mmMXBean.getMemoryPoolNames()).toString());
            System.out.println("==========================================");
        }
    }

    public static void getOsInfo(){
        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
        System.out.println("======osMXBean=======");
        System.out.println("getVersion="+osMXBean.getVersion());
        System.out.println("getName="+osMXBean.getName());
        System.out.println("getArch="+osMXBean.getArch());
        System.out.println("getAvailableProcessors="+osMXBean.getAvailableProcessors());
        System.out.println("getSystemLoadAverage="+osMXBean.getSystemLoadAverage());
        System.out.println("==========================================");
    }

    public static void getRuntimeMXBean(){
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println("============runtimeMXBean=============");
        System.out.println("getName="+runtimeMXBean.getName());
        System.out.println("getBootClassPath="+runtimeMXBean.getBootClassPath());
        System.out.println("getClassPath="+runtimeMXBean.getClassPath());
        System.out.println("getLibraryPath="+runtimeMXBean.getLibraryPath());
        System.out.println("getManagementSpecVersion="+runtimeMXBean.getManagementSpecVersion());
        System.out.println("getSpecName="+runtimeMXBean.getSpecName());
        System.out.println("getSpecVendor="+runtimeMXBean.getSpecVendor());
        System.out.println("getSpecVersion="+runtimeMXBean.getSpecVersion());
        System.out.println("getVmName="+runtimeMXBean.getVmName());
        System.out.println("getVmVendor="+runtimeMXBean.getVmVendor());
        System.out.println("getVmVersion="+runtimeMXBean.getVmVersion());
        System.out.println("getStartTime="+runtimeMXBean.getStartTime());
        System.out.println("getSystemProperties="+runtimeMXBean.getSystemProperties());
        System.out.println("==========================================");
    }
}
